package com.accp.controller;

import com.accp.pojo.Author;
import com.accp.service.AuthorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminCotrooller {
    Md5 md5 = new Md5();
    @Autowired
    private AuthorService authorService;

    @RequestMapping(value = "/cha")
    public String Cha(Model model) {
      /*  List<Author> aa = authorService.
        model.addAttribute("list1", aa);*/
        return "Login";
    }

    /**
     * 注册方法
     *
     * @param model
     * @param request
     * @return
     * @throws UnsupportedEncodingException
     * @throws NoSuchAlgorithmException
     */
    @RequestMapping(value = "/register")
    public String Register(Model model, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        Author author = new Author();
        author.setLoginname(request.getParameter("loginName"));
        author.setLoginpwd(md5.EncoderByMd5(request.getParameter("loginPwd")));
        author.setRealname(request.getParameter("username"));
        author.setEmail(request.getParameter("email"));
        author.setAuthortypeid(1);
        int row = authorService.insertSelective(author);
        if (row == 1) {
            System.out.println("<script>alert('注册成功，请登陆');</script>");
            return "Login";
        } else {
            System.out.println("<script>alert('注册失败');</script>");
            return "Login";
        }
    }

    /**
     * 注册验证用户名
     *
     * @param model
     * @param request
     */
    @RequestMapping(value = "RegisterName")
    public void RegisterName(Model model, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String loginName = request.getParameter("name");
        List<Author> listName = authorService.VerificationName(loginName);
        if (null == listName || listName.size() == 0) {
            response.getWriter().write("用户名已存在");
        } else {
            response.getWriter().write("用户名不存在");
        }
    }

    @RequestMapping(value = "Signin")
    public String Signin(HttpSession session, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        String loginName = request.getParameter("name");
        String loginPwd = request.getParameter("name");
        Author author1 = authorService.Signin(loginName, loginPwd);
        if (md5.checkpassword(loginPwd, author1.getLoginpwd())) {
            System.out.println("<script>alert('登陆成功');</script>");
            session.setAttribute("author", author1);
            return "index";
        } else {
            System.out.println("<script>alert('登录失败');</script>");
        }
        return "Login";
    }
}
